<template>
  <el-dialog
    title="车辆处置申请"
    ref="tableDialog"
    :visible="dialogVisible"
    width="80%"
    v-if="dialogVisible"
    @close="close"
  >
    <el-form :model="formData" label-width="120px" :rules="formRules" ref="form">
      <el-row>
        <el-col>
          <el-form-item label="申请原因" prop="reason">
            <el-input type="textarea" v-model="formData.reason" placeholder="请输入申请原因"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col>
          <el-form-item label="处置方法" prop="disposeId">
            <el-select v-model="formData.disposeId" placeholder="请选择处置方法" style="width: 100%;">
              <el-option
                :label="item.name"
                :value="item.id"
                v-for="(item, index) in czff"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col>
          <el-form-item label="请选择车辆" prop="carid">
            <el-row>
              <el-button type="primary" size="mini" @click="openCarListDialog">选择车辆</el-button>
            </el-row>
            <el-table :data="tableData" border style="width: 100%" background>
              <el-table-column fixed type="index" label="序号" align="center" width="60"></el-table-column>
              <el-table-column prop="carCode" label="车牌号" align="center"></el-table-column>
              <el-table-column prop="useType" label="车辆使用性质" align="center"></el-table-column>
              <el-table-column prop="pinpai" label="品牌" align="center"></el-table-column>
            </el-table>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="上传附件" prop="fileurl">
            <el-upload
              class="upload-demo"
              drag
              :action="webUploadUrl"
              :on-success="onSuccess1"
              :limit="1"
              :on-exceed="onExceed"
              :on-remove="onRemove1"
              :headers="{'Authorization':token}"
            >
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">
                将文件拖到此处，或
                <em>点击上传</em>
              </div>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer" style="text-align: left;">
      <el-button type="primary" @click="save">保存</el-button>
      <el-button @click="resetForm">重置</el-button>
    </span>
    <carlist ref="carlistComp"></carlist>
  </el-dialog>
</template>

<script>
import { getProperties } from "@/api/dictionary";
import { listJson } from "@/api/car";
import { apply } from "@/api/carcz";
import { mapGetters } from "vuex";
import carlist from "./carlist";
export default {
  components: { carlist },
  props: {
    dialogVisible: {
      type: Boolean,
      default: true
    }
  },
  data: function() {
    return {
      formData: {
        disposeId: null,
        fileurl: null,
        filename: null,
        reason: null,
        carid: null,
        carCode: null,
        keywords: ""
      },
      webUploadUrl: "", //文件上传地址
      tableData: [],
      propers: [], //单位性质
      standards: [], //单位规格
      czff: [],
      total: 0,
      pageNo: 1,
      pageSize: 10,
      //表单验证
      formRules: {
        reason: [
          {
            required: true,
            message: "请输入申请原因",
            trigger: "blur"
          }
        ],
        disposeId: [
          {
            required: true,
            message: "请选择处置方式",
            trigger: "blur"
          }
        ],
        fileurl: [
          {
            required: true,
            message: "请上传申请单",
            trigger: "blur"
          }
        ],
        carid: [
          {
            required: true,
            message: "请选择处置车辆",
            trigger: "blur"
          }
        ]
      }
    };
  },
  computed: {
    ...mapGetters(["userInfo", "token"])
  },
  created() {
    this.webUploadUrl = this.VUE_APP_BASE_API + "/common/webupload.shtml";
    this.getProperties();
  },
  methods: {
    //打开车辆选择页面
    openCarListDialog() {
      this.$refs.carlistComp.open();
    },
    //子组件完成车辆选择的回调
    onCarSelect(carData) {
      this.tableData.splice(0, this.tableData.length, carData);
      this.formData.carid = carData.id;
    },
    onExceed() {
      this.$message("只能上传1个文件，请先移除已上传文件再上传");
    },
    onSuccess1(file) {
      if (!file) return;
      console.log(file);
      if (file.error == 0) {
        this.formData.fileurl = file.url;
        this.formData.filename = file.filename;
      } else {
        this.$message("上传失败，请重试");
      }
    },
    onRemove1() {
      this.applyForm.bzwjurl = "";
    },
    //获取处置类型
    async getProperties() {
      try {
        let r = await getProperties({ baseTypeId: 32 });
        if (r && r.error == 0) {
          this.czff.splice(0, this.czff.length, ...r.data);
          this.formData.disposeId = r.data[0].id;
          console.log(this.czff)
          this.czff.forEach(function (e, inx) {
            if(e.name == "调剂"){
              e.name = "上交"
            }
          })
        } else {
          this.$message({
            message: "获取数据失败",
            type: "error"
          });
        }
      } catch (error) {
        this.$message({
          message: "获取数据失败",
          type: "error"
        });
      }
    },
    resetForm() {
      this.$refs.form.resetFields();
    },
    save() {
      this.$refs.form.validate(async pass => {
        if (!pass) {
          return;
        }
        try {
          let r = await apply(this.formData);
          if (r && r.error == 0) {
            this.$message({
              message: "提交成功",
              type: "success"
            });
            this.close();
          } else {
            this.$message({
              message: "出错了",
              type: "error"
            });
          }
        } catch (error) {
          this.$message({
            message: "出错了",
            type: "error"
          });
        }
      });
    },
    close() {
      this.$emit("update:dialogVisible", false);
      this.$parent.fetchData();
    },
    handleCurrentChange(val) {
      this.formData.carid = val.id;
      this.formData.carCode = val.carCode;
      console.log(this.formData);
    }
  }
};
</script>

<style lang="scss" scoped>
.s_title {
  background: #f6f6f6;
  height: 40px;
  line-height: 40px;
  padding-left: 10px;
  margin-top: 0;
}
</style>